import type { Request, Response } from "express";
import {
  getGameTimeEngine,
  type GameTimeSpeed,
  type GameTimeStatus,
} from "../services/GameTime";

/**
 * GET /api/game/time
 * 获取当前游戏时间状态
 */
export const getGameTime = (_req: Request, res: Response) => {
  const engine = getGameTimeEngine();
  const state = engine.getState();

  res.json({
    gameDate: state.gameDate.toISOString(),
    gameDateFormatted: engine.formatDate(),
    speed: state.speed,
    status: state.status,
    isTradingDay: engine.isTradingDay(),
    isTradingHours: engine.isTradingHours(),
    timeToNextMarketOpen: engine.getTimeToNextMarketOpen(),
    startDate: state.startDate.toISOString(),
    gameTimeElapsed: engine.getGameTimeElapsed(),
    realTimeElapsed: engine.getRealTimeElapsed(),
  });
};

/**
 * POST /api/game/time/speed
 * 设置游戏时间速度
 * body: { speed: 1 | 2 | 5 | 10 | 20 | 50 | 100 }
 */
export const setGameTimeSpeed = (req: Request, res: Response) => {
  const { speed } = req.body as { speed?: number };

  const validSpeeds: GameTimeSpeed[] = [1, 2, 5, 10, 20, 50, 100, 200, 500];
  if (!speed || !validSpeeds.includes(speed as GameTimeSpeed)) {
    return res.status(400).json({
      error: `speed 必须是以下值之一: ${validSpeeds.join(", ")}`,
    });
  }

  const engine = getGameTimeEngine();
  engine.setSpeed(speed as GameTimeSpeed);

  const state = engine.getState();
  res.json({
    message: "时间速度已更新",
    speed: state.speed,
    gameDate: state.gameDate.toISOString(),
    gameDateFormatted: engine.formatDate(),
  });
};

/**
 * POST /api/game/time/pause
 * 暂停游戏时间
 */
export const pauseGameTime = (_req: Request, res: Response) => {
  const engine = getGameTimeEngine();
  engine.pause();

  res.json({
    message: "游戏时间已暂停",
    status: "paused" as GameTimeStatus,
    gameDate: engine.getGameDate().toISOString(),
    gameDateFormatted: engine.formatDate(),
  });
};

/**
 * POST /api/game/time/resume
 * 继续游戏时间
 */
export const resumeGameTime = (_req: Request, res: Response) => {
  const engine = getGameTimeEngine();
  engine.resume();

  res.json({
    message: "游戏时间已继续",
    status: "playing" as GameTimeStatus,
    gameDate: engine.getGameDate().toISOString(),
    gameDateFormatted: engine.formatDate(),
  });
};

/**
 * POST /api/game/time/toggle
 * 切换暂停/继续状态
 */
export const toggleGameTime = (_req: Request, res: Response) => {
  const engine = getGameTimeEngine();
  engine.toggle();

  const state = engine.getState();
  res.json({
    message: `游戏时间已${state.status === "playing" ? "继续" : "暂停"}`,
    status: state.status,
    gameDate: state.gameDate.toISOString(),
    gameDateFormatted: engine.formatDate(),
  });
};

/**
 * POST /api/game/time/jump
 * 跳转到指定日期
 * body: { date: "2035-06-21T09:00:00" }
 */
export const jumpToDate = (req: Request, res: Response) => {
  const { date } = req.body as { date?: string };

  if (!date) {
    return res.status(400).json({ error: "date 是必填项" });
  }

  const targetDate = new Date(date);
  if (isNaN(targetDate.getTime())) {
    return res.status(400).json({ error: "无效的日期格式" });
  }

  const engine = getGameTimeEngine();
  engine.jumpToDate(targetDate);

  res.json({
    message: "已跳转到指定日期",
    gameDate: engine.getGameDate().toISOString(),
    gameDateFormatted: engine.formatDate(),
  });
};

/**
 * POST /api/game/time/skip-day
 * 跳过当前交易日，直接到下一个交易日开盘
 */
export const skipToNextTradingDay = (_req: Request, res: Response) => {
  const engine = getGameTimeEngine();
  engine.skipToNextTradingDay();

  const state = engine.getState();
  res.json({
    message: "已跳转到下一个交易日",
    gameDate: state.gameDate.toISOString(),
    gameDateFormatted: engine.formatDate(),
    isTradingDay: engine.isTradingDay(),
    isTradingHours: engine.isTradingHours(),
  });
};




